/**
 * Created by kangmm on 2016/9/26 0026. 定义服务接口路径
 */
window.App = new Object();
// api接口路径
App.apiUrl = {
  aicIndex: '', //
};
// 根据api名称获取对应的URL
App.getApiUrl = function(apiName) {
  var baseUrl = App.envUrl.baseUrl;
  if (baseUrl == undefined || baseUrl == '') {
    baseUrl = 'http://' + window.location.host + '/';
    if (window.location.href.split('/').length >= 4) {
      baseUrl += window.location.href.split('/')[3] + '/';
    }
  }
  console.log(baseUrl + App.apiUrl[apiName]);
  //return baseUrl + App.apiUrl[apiName];
  return baseUrl + App.apiUrl[apiName] + '?x-auth-token=' + sessionStorage.token;
  // return "http://10.1.103.77:8080/" + App.apiUrl[apiName];
};

App.getBaseUrl = function() {
  var baseUrl = App.envUrl.baseUrl;
  if (baseUrl == undefined || baseUrl == '') {
    baseUrl = 'http://' + window.location.host + '/';
    if (window.location.href.split('/').length >= 4) {
      baseUrl += window.location.href.split('/')[3] + '/';
    }
  }
  return baseUrl;
};

App.exApiUrl = {
  proposalIndex: 'index.html',
  activityCenter: 'agent/center.html',
  activityClient: 'agent/client.html',
  query_my_customer: 'query_my_customer', //获取客户列表接口
  query_activity_info: 'query_activity_info', //活动
  download_data: 'api/activity/download_data', //获取客户列表
  get_custinfo_by_id: 'get_custinfo_by_id', //获取客户详细信息
  update_customer_info: 'update_customer_info', //切换客户类型
  wxShareURL: 'api/count/wx_share_config', //微信分享配置接口
  query_product_info: 'api/sales/product', //产品
};

App.getExApiUrl = function(exUrl, apiName) {
  console.log('1111');
  return App.envUrl[exUrl] + App.exApiUrl[apiName];
};

/**
 * 获取URL参数
 *
 * @return {String} name
 */
App.getmtParam = function(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = window.location.search.substr(1).match(reg);
  if (r) {
    return decodeURI(r[2]);
  }
  return null;
};

/**
 * 获取URL参数
 *
 * @return {String} name
 */
App.getParam = function(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = window.location.search.substr(1).match(reg);
  if (r) {
    return decodeURI(r[2])
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');
  }
  return null;
};

/**
 * 获取编码后的参数字符串
 *
 * @param {string}
 *            url 传入的URL
 * @param {sting}
 *            encodeParam 指定的需要编码的参数
 */
App.encodeParams = function(url, encodeParam) {
  var pairs = [],
    map = [],
    str = '',
    value;
  pairs = url.split('&');
  for (var i = 0, len = pairs.length; i < len; i++) {
    value = pairs[i].charAt(0) == '?' ? pairs[i].replace(/\?/, '') : pairs[i];
    // map = pairs[i].split('=');
    // 获得参数键值对
    map[0] = value.substr(0, value.indexOf('='));
    map[1] = value.substr(value.indexOf('=') + 1, value.length);
    // 对指指定参数进行编码
    $.each(encodeParam, function(i, param) {
      if (map[0] == param) {
        map[1] = encodeURIComponent(map[1]);
      }
    });
    // 拼接参数串
    if (map[0] && i < len - 1) {
      str += map[0] + '=' + map[1] + '&';
    } else {
      str += map[0] + '=' + map[1];
    }
  }

  return '?' + str;
};

/**
 * 判断当前是否在微信内置浏览器
 */
App.isWeiXin = function() {
  var ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    return true;
  } else {
    return false;
  }
};

/**
 * 判断修改页面标题
 */
App.setTitle = function(title) {
  // if(App.env == "Test") {
  //   document.title = title + "(测试)";
  // } else {
  document.title = title;
  // }
};
/*格式化时间*/
Date.prototype.Format = function(fmt) {
  var o = {
    'M+': this.getMonth() + 1, //月份
    'd+': this.getDate(), //日
    'h+': this.getHours(), //小时
    'm+': this.getMinutes(), //分
    's+': this.getSeconds(), //秒
    'q+': Math.floor((this.getMonth() + 3) / 3), //季度
    S: this.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp('(' + k + ')').test(fmt))
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
  return fmt;
};
/*ios微信浏览器上下滚动兼容性问题解决 end*/
App.wxScrollSolve = function(scrollWrapObj) {
  //Scrollobj要滚动的内容外部包裹的容器对象
  if (scrollWrapObj == '' || scrollWrapObj == undefined || scrollWrapObj == null) {
    return;
  }
  var overscroll = function(el) {
    el.addEventListener('touchstart', function() {
      var top = el.scrollTop,
        totalScroll = el.scrollHeight,
        currentScroll = top + el.offsetHeight;
      if (top === 0) {
        el.scrollTop = 1;
      } else if (currentScroll === totalScroll) {
        el.scrollTop = top - 1;
      }
    });
    el.addEventListener('touchmove', function(evt) {
      if (el.offsetHeight < el.scrollHeight) evt._isScroller = true;
    });
  };
  overscroll(scrollWrapObj); /*document.querySelector('.MainCon')*/
  document.body.addEventListener('touchmove', function(evt) {
    if (!evt._isScroller) {
      evt.preventDefault();
    }
  });
};
/*随机生成字符串*/
App.rendomStr = function(len) {
  len = len || 8;
  let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678';
  let MaxLen = chars.length;
  let rendomStrRes = '';
  for (let i = 0; i < len; i++) {
    rendomStrRes += chars.charAt(Math.floor(Math.random() * MaxLen));
  }
  return rendomStrRes;
};
